package com.kd.d5000acquire.dm;

import com.kd.d5000acquire.schedule.SqlSchedule;
import com.kd.d5000acquire.server.AppStatusServer;
import com.kd.d5000acquire.util.DMUtil;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
@ConfigurationProperties(prefix = "my")
@Data
public class AllDao implements Runnable{

    @Autowired
    private DMUtil dmUtil;
    @Autowired
    private AppStatusServer appStatusServer;

    private Map<String,Map<String,String>> servers;

    private ScheduledExecutorService service = Executors.newScheduledThreadPool(15);

    @SneakyThrows
    @Scheduled(fixedDelay = 30000)
    @Override
    public void run() {
        for (Map.Entry<String, Map<String, String>> server : servers.entrySet()) {
            String name = server.getKey();
            Map<String, String> value = server.getValue();
            String sql = value.get("sql");
            String definitionId = value.get("definitionId");
            Integer intervals = Integer.valueOf(value.get("intervals"));
            Integer delay = Integer.valueOf(value.get("delay"));

            SqlSchedule sqlSchedule = new SqlSchedule(sql,name,definitionId,intervals);
            service.scheduleAtFixedRate(sqlSchedule,10,delay, TimeUnit.SECONDS);
        }
        while (!service.isShutdown()) {
            Thread.sleep(100000);
        }
    }



}
